package tec;

class TestAutobus {
  void testFaussaire() {
    Passager f = new FauxPassager();


    assert 0 == ((FauxPassager)f).messages.size();

    ((FauxPassager)f).nouvelArret(null, -999);

    assert "ajoute nouvel arret" == ((FauxPassager)f).messages.getLast();
  }

  void lancer() { 
    int nbTest = 0;
	System.out.println("Test de Autobus:");
		
    System.out.print('.'); nbTest++;
    testFaussaire();
    System.out.print('.'); nbTest++;
    testAPlaceAssise();
    System.out.print('.'); nbTest++;
    testAPlaceDebout();
    System.out.print('.'); nbTest++;
    testDemanderPlaceAssise();
    System.out.print('.'); nbTest++;
    testDemanderPlaceDebout();
    System.out.print('.'); nbTest++;
    testDemanderChangerEnDebout();
    System.out.print('.'); nbTest++;
    testDemanderChangerEnAssis();
    System.out.print('.'); nbTest++;
    testDemanderSortie();
    System.out.print('.'); nbTest++;
    testExceptionAllerArretSuivant();
    System.out.print('.'); nbTest++;
    testSortieListe();

    System.out.println("(" + nbTest + "):OK: "); 
  }

  void testAPlaceAssise() {
      Bus bus = new Autobus(1,0);
      assert bus.aPlaceAssise();
      Bus bus2 = new Autobus(0,0);
      assert !(bus2.aPlaceAssise());
  }

  void testAPlaceDebout() {
      Bus bus = new Autobus(0,1);
      assert bus.aPlaceDebout();
      Bus bus2 = new Autobus(0,0);
      assert !(bus2.aPlaceDebout());
  }

  void testDemanderPlaceAssise() {
      Passager p = new FauxPassager();
      Bus bus = new Autobus(1,0);
      bus.demanderPlaceAssise(p);
      assert !(bus.aPlaceAssise());
      assert ((FauxPassager)p).messages.getLast() == "accepte place assise";
  }

  void testDemanderPlaceDebout() {
      Passager p = new FauxPassager();
      Bus bus = new Autobus(0,1);
      bus.demanderPlaceDebout(p);
      assert !(bus.aPlaceAssise());
      assert ((FauxPassager)p).messages.getLast() == "accepte place debout";
  }

  void testDemanderChangerEnDebout() {
    Bus bus = new Autobus(0,1);
    Passager p = new FauxPassager();
    bus.demanderChangerEnDebout(p);
    assert bus.aPlaceAssise();
    assert !(bus.aPlaceDebout());
    assert ((FauxPassager)p).messages.getLast() == "accepte place debout";
  }

  void testDemanderChangerEnAssis() {
    Bus bus = new Autobus(1,0);
    Passager p = new FauxPassager();
    bus.demanderChangerEnAssis(p);
    assert bus.aPlaceDebout();
    assert !(bus.aPlaceAssise());
    assert ((FauxPassager)p).messages.getLast() == "accepte place assise";
  }

  void testDemanderSortie() {
    Bus bus = new Autobus(0,0);
    Bus bus2 = new Autobus(0,0);
    Passager p = new FauxPassager();
    Passager p2 = new FauxPassager();
    bus.demanderSortie(p);
    bus2.demanderSortie(p2);

    assert ((FauxPassager)p).messages.getLast() == "accepte sortie";
    assert ((FauxPassager)p2).messages.getLast() == "accepte sortie";
  }
  public void testExceptionAllerArretSuivant(){
    Autobus bus = new Autobus(1,1);
    Passager p = new FauxPassager() {
      public void nouvelArret(Bus bus, int numeroArret) throws IllegalStateException {
          throw new IllegalStateException();
      }
    };
    boolean test = true;
	 try {
		bus.demanderPlaceAssise(p);
		bus.allerArretSuivant();
    
    } catch (TecInvalidException t) {
		test = false;
    }
	assert test == false : "exception non levée";
  }
  
   public void testSortieListe() {
	Autobus bus = new Autobus(1,1);
	Passager p = new FauxPassager() {
		  public void nouvelArret(Bus bus, int numeroArret) {
			  bus.demanderSortie(this);
		  }
		};
	try {
		bus.demanderPlaceAssise(p);
		assert bus.estDansBus(p) : "pas rentre";
		bus.allerArretSuivant();
		assert ((bus.estDansBus(p)) == false) : "pas sorti";
    } 
    catch (TecInvalidException t) {}
  }
}
